package com.hotpot.quartz;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.Job;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;

/**
 * 描述:定时任务插件
 * @author myx
 * @date 2017年6月23日 下午5:09:42
 */
public class QuartzPlugin  implements IPlugin{
	
	private Logger log = LogManager.getLogger(QuartzPlugin.class);
	/**
	 * 配置文件地址默认 quartz.properties
	 */
	private String filePath="quartz.properties";
	
	/**
	 * 定时任务组名称默认为quartz
	 */
	private String groupName="quartz";

	/**
	 * @param filePath 定时任务配置文件地址,默认 quartz.properties
	 * @param groupName 定时任务组名称,默认为quartz
	 */
	public QuartzPlugin(String filePath,String groupName) {
		this.filePath=filePath;
		this.groupName=groupName;
	}
	/**
	 * @Title:QuartzPlugin
	 * @Description:默认构造方法
	 */
	public QuartzPlugin() {
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean start() {
		boolean startSuccess=true;
		try {
			QuartzManager qm = QuartzManager.getIstance();
			String names = PropKit.use(filePath).get(groupName);
			String[] nameList = names.split(",");
			for (String name : nameList) {
				try {
					System.out.println(PropKit.use(filePath).get(name+ ".class"));
					if(PropKit.use(filePath).getBoolean(name+ ".enable")){
						qm.addJob(name,groupName,(Class<Job>) Class.forName(PropKit.use(filePath).get(name+ ".class")), PropKit.use(filePath).get(name + ".cron"));
					}
				} catch (Exception e) {
					startSuccess = false;
					log.error("=======定时器任务"+name+"出错========");
					e.printStackTrace();
				}
			}
			qm.startQuartz();
		} catch (Exception e) {
			startSuccess=false;
			e.printStackTrace();
		}
		return startSuccess;
	}

	@Override
	public boolean stop() {
		return false;
	}

}
